      SUBROUTINE SUNDEP(ITEST,SB)
      IMPLICIT REAL*8(A-H,O-Z)
C
C     THIS SUBROUTINE SERVES TWO FUNCTIONS
C
C     ITEST=1  IS ACALL FROM FNDALP WITH SB EQUAL TO THE UNIT
C     SUN VECTOR IN THE BODY FRAME. THE SUBROUTINE DETERMINES
C     WHETHER THE COMPONENT SB(IC)  HAS GONE THROUGH ZERO IN THE
C     TIME BETWEEN THE LAST TIME AND THE PRESENT TIME. IF CONDITIONS
C     ARE MET, THE CROSSING IS COUNTED AND THE TIME OF CROSSING AND
C     SPIN RATE ARE STORED
C
C
C     ITEST=2  IS A CALL FROM MAIN AT PTINT TIME WITH SB INDIFFERENT.
C     IF THE NUMBER OF CROSSINGS IS ONE LESS THAN THE SPECIFIED
C     NUMBER OF CROSSINGS, THE NEW STOP TIME AND NEW PRINT FREQUENCY
C     ARE DETERMINED. THE INDEX JTEST IS SET SO THAT NO MORE
C     COMPUTATIONS ARR DONE BY THIS SUBROUTINE.
C
      COMMON/CODPLY/STANG,ANGTOL
C
      COMMON/CONSTS/PI,TWOPI,RADIAN
C
      COMMON/IODPLY/ISDPLY,IRAXIS,ISAXIS,NCROSS,NPRINT
C
      COMMON/IPOOL1/ IGRAV,IDAMP,IK,K1,ITIM,IAB,IAPS,IBB,IBPS,NK(10),
     .               LK(10),LLK(10)
C
      COMMON/PRCOM /STORE(10,30),ILINE,ICOL,ICNT,IHDD
C
      COMMON/RMAIN1/ DELTAT,FACTOR,FREQ,TSTOP,DELMIT,
     .               UPBND(150),DNBND(150)
C
      COMMON/RPOOL1/ RHOK(10),TIME,SA(3,3),FM1(3,3),ZLK(10),OMEG(3),
     .               ZLKP(10),ZLKDP(10),CMAT(3,3),GBAR(3,3),YBCM(3),
     .               ZBZK(3,10),FCM(3,3),DTO,PHID,PHI
C
      COMMON/RPOOL2/ PO,SD(3),DAN(3,10),DBN(3,10),CFMT(3,3),DIY1(3),
     .               SD1(3),DT1,P1,AERO,DTO1,YIZK(3),PO1
C
      DIMENSION TCROSS(10),OMEGCR(10),TAU(9),SB(3),ZSUN(3),JJ(3),KK(3)
C
      IF(ITEST.EQ.2) GO TO 100
C     COMPUTATIONS AT INITIAL TIME ONLY
      IF(ITIM.EQ.2) GO TO 10
      JJ(1)=2
      JJ(2)=3
      JJ(3)=1
      KK(1)=3
      KK(2)=1
      KK(3)=2
      ICROSS=0
      JTEST=0
      IWR=1
      IR=IRAXIS
      IS=ISAXIS
      IF(IS.EQ.IR) IS=JJ(IR)
      IC=JJ(IR)
      IF(IC.EQ.IS) IC=KK(IR)
      IT=JJ(IR)
      SLAST=2.0D0
      TDEL=TIME+4.0D0*DELTAT
      WS1=SB(IS)
      WS2=SB(IC)
      WS3=WS1*WS1+WS2*WS2
      IF(WS3.GT.3.0D-4) GO TO 10
C     IF THE SUN VECTOR LIES WITHIN A DGGREE OF THE SPIN AXIS
C     THE COMPUTATIONS ARE NOT PERFORMED AND THE RUN WILL BE STOPPED.
      TSTOP=TDEL
      FREQ=TDEL-TIME
      JTEST=1
      WRITE(6,202)
      RETURN
   10 CONTINUE
      DO 12 M=1,3
   12 ZSUN(M)=SB(M)
      IF(JTEST.NE.0) RETURN
      IF(TIME.LE.TDEL) RETURN
      SNOW=SB(IC)
      TNOW=TIME
      OMNOW=OMEG(IR)
      IF(SLAST.EQ.2.0D0) GO TO 30
      IF(TIME.LE.TLAST) GO TO 30
      IF(OMNOW.LT.0.0D0) GO TO 20
      IF(IC.EQ.IT) GO TO 22
   14 CONTINUE
      IF(SLAST.GT.0.0D0) GO TO 30
      IF(SNOW.LE.0.0D0) GO TO 30
   15 CONTINUE
      WS1=SNOW-SLAST
      WS2=SLAST/WS1
      WS1=TNOW-TLAST
      WS3=DABS(WS2)
      WS2=WS1*WS3
      ICROSS=ICROSS+1
      I1=ICROSS
      IF(ICROSS.LE.10) GO TO 18
      I1=10
      DO 17 I=1,9
      J=I+1
      TCROSS(I)=TCROSS(J)
      OMEGCR(I)=OMEGCR(J)
   17 CONTINUE
   18 CONTINUE
      TCROSS(I1)=TLAST+WS2
      WS2=OMNOW-OMLAST
      OMEGCR(I1)=OMLAST+WS2*WS3
      GO TO 30
   20 CONTINUE
      IF(IC.EQ.IT) GO TO 14
   22 CONTINUE
      IF(SLAST.LT.0.0D0) GO TO 30
      IF(SNOW.LE.0.0D0) GO TO 15
   30 CONTINUE
      TLAST=TNOW
      SLAST=SNOW
      OMLAST=OMNOW
      RETURN
  100 CONTINUE
      IF(JTEST.NE.0) RETURN
      IF(NCROSS.LT.3) NCROSS=3
      I1=ICROSS+1
      IF(I1.LT.NCROSS) RETURN
      I1=ICROSS
      IF(I1.GT.10) I1=10
      I2=I1-1
      DO 101 I=1,I2
      TAU(I)=TCROSS(I+1)-TCROSS(I)
  101 CONTINUE
      IF(I2.EQ.1) GO TO 103
      I3=I2-1
      ER1=0.0D0
      DO 102 I=1,I3
      J=I+1
      WS1= TAU(J)-TAU(I)
      WS2=TAU(J)+TAU(I)
      WS3=DABS(WS1)
      ER=2.0D0*TWOPI*WS3/WS2
      IF(ER.GT.ER1) ER1=ER
  102 CONTINUE
      TE=(TWOPI+STANG*RADIAN)*ER1/TWOPI
      TOL=ANGTOL*RADIAN
      IF(TE.GT.TOL) IWR=2
      GO TO 110
  103 CONTINUE
      WS1=TWOPI/OMEGCR(I1)
      WS2=DABS(WS1)
      WS2=WS2+TAU(I2)
      ER1=2.0D0*TWOPI*WS1/WS2
      GO TO 102
  110 CONTINUE
      JTEST=1
      WS1=TIME-TCROSS(I1)
      WS2=TAU(I2)*STANG/360.0D0
      WS2=WS2+TAU(I2)-WS1
      WS1=NPRINT
      FREQ=WS2/WS1
      TSTOP=TIME+WS2
      IF(ICNT.GT.55) GO TO 120
      ICNT=ICNT+1
      WS1=DMOD(TSTOP,8.64D4)
      WS2=HMSOUT(WS1)
      WRITE(6,200) WS2,FREQ
      ICNT=ICNT+1
      IF(IWR.EQ.1) GO TO 120
      WRITE(6,201)
  120 CONTINUE
      I1=ICROSS-1
      WRITE(6,251) (TCROSS(J),J=I1,ICROSS)
      WRITE(6,252) (ZSUN(K),K=1,3)
      WRITE(6,253) (SD(L),L=1,3)
      RETURN
  200 FORMAT('0',3X,'STOPPING AT ',F10.3,' WITH PRINT FREQUENCY ',G15.5)
  201 FORMAT('0',3X,' THE TOLERANCE MAY BE EXCEEDED ')
  202 FORMAT('0',3X,' THE SUN LINE IS ALONG THE SPIN AXIS ')
  251 FORMAT('   TCROSS= ',1(1PE16.8))
  252 FORMAT('   SB(BODY)',3(1PE16.8))
  253 FORMAT('   SD(INERTIAL)',3(1PE16.8))
      END
